using System.Collections.Generic;
using System.IO;
using JsonRpcGen.TypeHandler;
using Newtonsoft.Json.Linq;

namespace JsonRpcGen.ReturnHandler
{
    internal class ObjectReturnHandler : ReturnHandler
    {
        private readonly JProperty _method;
        private JToken _properties;
        private string _methodName;
        private bool _done;

        public ObjectReturnHandler(JProperty method) : base(method.Name+"Response")
        {
            _method = method;
            var parts = method.Name.Split('.');
            _methodName = parts[1];
            _properties = _method.First["returns"]["properties"];
            if (_properties==null)
            {
                Name = "JObject";
            }
            else
            {
                Name = _methodName + "Response";
            }
        }

        public override void WriteType(StreamWriter writer, Dictionary<string, string> replacements)
        {
            if (!_done)
            {


                if (_properties != null)
                {
                    var typeHandler = new ObjectTypeHandler(_properties);
                    typeHandler.WriteClass(writer, Name, replacements);
                }
                _done = true;
            }
        }

    }
}